package com.aizhixin.lab.project.template.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.project.template.domain.PostInfoDomain;
import com.aizhixin.lab.project.template.domain.ProjectPostDomain;
import com.aizhixin.lab.project.template.domain.ProjectSkillDomain;
import com.aizhixin.lab.project.template.entity.ProjectCourse;
import com.aizhixin.lab.project.template.entity.ProjectModule;
import com.aizhixin.lab.project.template.entity.ProjectPost;
import com.aizhixin.lab.project.template.entity.ProjectSkill;
import com.aizhixin.lab.project.template.repository.ProjectPostRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class ProjectPostService {
    @Autowired
    private ProjectPostRepository projectPostRepository;

    public ProjectPost save(ProjectPost domain) {
        if (null == domain.getId() || "".equals(domain.getId())) {
            domain.setId(UUID.randomUUID().toString());
        }
        return projectPostRepository.save(domain);
    }

    public List<ProjectPost> save(List<ProjectPost> domain) {

        return projectPostRepository.save(domain);
    }
    public List<ProjectPost> findProjectPostByPostId(String postId){
        return projectPostRepository.findByPostIdAndDeleteFlag(postId,DataValidity.VALID.getState());
    }


    public List<ProjectPostDomain> findProjectPost(String id) {
        List<ProjectPostDomain> projectPostDomainList = new ArrayList<>();
        List<ProjectPost> projectPosts = projectPostRepository.findByProjectIdAndDeleteFlag(id ,DataValidity.VALID.getState());
        for (ProjectPost post : projectPosts) {
            ProjectPostDomain domain = new ProjectPostDomain();
            BeanUtils.copyProperties(post, domain);
            projectPostDomainList.add(domain);
        }
        return projectPostDomainList;
    }

//    public List<ProjectSkillDomain> findSkillByPost(String id) {
//
//        return findPostInfo(id).getProjectPostDomainList();
//    }

    public List<ProjectPost> findProjectPostList(String projectId) {
        return projectPostRepository.findByProjectIdAndDeleteFlag(projectId, DataValidity.VALID.getState());
    }

    public PostInfoDomain findPostInfo(String id) {
        PostInfoDomain postInfoDomain = new PostInfoDomain();
        ProjectPost post = projectPostRepository.findOne(id);
//        List<ProjectSkillDomain> projectSkillDomainList= new ArrayList<>();

//        for (TeacherProjectSkill projectSkill:post.getProjectSkills()){
//            ProjectSkillDomain projectSkillDomain = new ProjectSkillDomain();
//            BeanUtils.copyProperties(projectSkill,projectSkillDomain);
//            projectSkillDomainList.add(projectSkillDomain);
//        }
        BeanUtils.copyProperties(post, postInfoDomain);
//        postInfoDomain.setProjectPostDomainList(projectSkillDomainList);
        return postInfoDomain;
    }
    public Map<String, Object> delete(String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        ProjectPost projectPost = projectPostRepository.findOne(id);
        if (!Objects.isNull(projectPost)) {
            projectPost.setDeleteFlag(DataValidity.INVALID.getState());
            save(projectPost);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, "删除成功");
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "未找到课程信息");
        }
        return result;
    }


}
